<template>
  <Popup
    :round="round"
    v-model="visible"
    @click-overlay="handleClose"
    :close-on-click-overlay="false"
    :safe-area-inset-bottom="true"
    :transition-appear="true"
    :close-on-popstate="true"
    overlay-class="layer_wrap"
  >
    <div class="layer_inside">
      <div class="patient-info">
        <div class="patient-name" v-if="info?.treatingAlert">
          <template v-for="item in patientListInfo">
            {{item.orderNo}}{{item.patientName}}
          </template>
        </div>
        <div class="wait-patient-name" v-if="(info?.waitingAlert)&&(waitPatientListInfo?.length)">
          <template v-for="item in waitPatientListInfo">
            {{item.orderNo}}{{item.patientName}}
          </template>
        </div>
        <div class="current-patient" v-if="(BroadcastContent?.treatingAlert)">
          <div v-for="(item, index) in BroadcastContent?.currentContent" :key="index" :class="item['type']">
            <template v-if="item.type === 'patientName' || item.type === 'patientInfo'">
              <div class="patientName">
                <template v-if="true">
                  {{ item.name }}
                </template>
              </div>
            </template>
            <template v-if="item.type === 'text'">
              {{ item.name }}
            </template>
            <template v-if="item.type === 'treatmentRoomName' || item.type === 'deptName'">
              <div class="treatmentRoomName">
                {{ item.name }}
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </Popup>
</template>

<script>
import {Popup} from 'vant';
import {pxToVw} from "@/utils/common";
import Template from "@sc/template/index.vue";

export default {
  name: 'CallPatientCurrent',

  componentName: "CallPatientCurrent",

  props: {
    visible: {
      type: Boolean,
      required: false
    },
    info: {
      type: Object,
      required: true
    },
    checkList: {
      type: Array,
      required: true
    },
  },

  components: {
    Template,
    Popup,
  },

  data() {
    return {
      round: true,
    }
  },

  computed: {
    shownPatientInfo() {
      return function (rowList, key) {
        return this.$lodash.some(rowList, function (obj) {
          return obj['type'] === key;
        })
      }
    },

    // 候诊患者是否显示
    currentPatientShown() {
      let {treatingAlert, currentContent, currentPatientList} = (this.BroadcastContent);
      return treatingAlert && !!currentContent && !!(currentPatientList.length)
    },

    // 获取播报的内容
    BroadcastContent() {
      const self = this;
      let msgInfo = Object();
      let {data} = (self.info)
      let msgContent = data?.msgContent || Object();
      if (msgContent?.treatingVoiceContentConfig) {
        // 正在就诊的控制文字
        let treatingVoiceContentConfig = msgContent.treatingVoiceContentConfig;
        // 候诊语音播报的文案
        let waitingVoiceContent = msgContent.waitingVoiceContent;

        // 循环判断
        if (!!treatingVoiceContentConfig) {
          treatingVoiceContentConfig.forEach((item, index, arr) => {
            let key = item.type;
            let boolean = key === 'orderNo' || key === 'patientName' || key === 'treatmentRoomName' || key === 'deptName' || key === 'workerName';
            if (boolean) {
              switch (key) {
                case 'orderNo':
                case 'patientName':
                  let patientInfoArr = Array();
                  let temps = new Map();
                  temps.set('orderNo', {key: 'orderNo', state: this.shownPatientInfo(arr, 'orderNo')});
                  temps.set('patientName', {key: 'patientName', state: this.shownPatientInfo(arr, 'patientName')});
                  (msgContent.treatingPatientList).forEach((item) => {
                    let patientInfo = '';
                    let orderNoInfo = temps.get('orderNo');
                    let patientNameInfo = temps.get('patientName')
                    if (orderNoInfo.state) {
                      patientInfo = `${self.$lodash.trim(patientInfo)}${self.$lodash.trim(item['orderNo'])}`
                    }
                    if (patientNameInfo.state) {
                      patientInfo = `${self.$lodash.trim(patientInfo)}${self.$lodash.trim(item['patientName'])}`
                    }
                    patientInfoArr.push(patientInfo)
                  })
                  if(!this.shownPatientInfo(arr, 'patientInfo')){
                    let patientInfoStr = self.$lodash.join(patientInfoArr, '\u3000');
                    arr.splice(index + 1, 0, {type: 'patientInfo', name: patientInfoStr, tempName: patientInfoArr});
                  }
                  break
                case 'treatmentRoomName':
                  Reflect.set(item, 'name', msgContent.treatmentRoomName);
                  break
                case 'deptName':
                  Reflect.set(item, 'name', msgContent.deptName);
                  break
                case 'workerName':
                  Reflect.set(item, 'name', msgContent.workerName);
                  break
              }
            }
          });
        }

        // 判断是否展示全量的患者信息
        let hasPatientAllInfo = this.shownPatientInfo(treatingVoiceContentConfig, 'patientInfo');
        if (hasPatientAllInfo) {
          this.$lodash.remove(treatingVoiceContentConfig, obj => (obj['type'] === 'patientName') || (obj['type'] === 'orderNo') || (obj['type'] === 'hao'))
        }
        //  就诊语音的配置数值信息
        Reflect.set(msgInfo, 'treatingAlert', (msgContent.treatingAlert) || false)
        Reflect.set(msgInfo, 'waitingAlert', (msgContent.waitingAlert) || false)
        Reflect.set(msgInfo, 'visible', !!((msgContent.treatingAlert) || (msgContent.waitingAlert)))
        Reflect.set(msgInfo, 'waitContent', waitingVoiceContent)
        Reflect.set(msgInfo, 'waitPatientList', (msgContent.waitingPatientList) || [])
        Reflect.set(msgInfo, 'currentContent', treatingVoiceContentConfig || [])
        Reflect.set(msgInfo, 'currentPatientInfo', msgContent.treatingPatient)
      }
      return msgInfo;
    },

    //截取当前患者
    patientListInfo () {
      let currPatient = this.info['currPatient'];
      if (currPatient.length >= 10) {
        return currPatient.slice(0, 10)
      } else {
        return currPatient
      }
    },

    // 获取候诊患者
    waitPatientListInfo (){
      let waitPatient = this.info['waitPatient'];
      if (waitPatient.length >= 10) {
        return waitPatient.slice(0, 10)
      } else {
        return waitPatient
      }
    }
  },

  mounted() {
  },

  methods: {
    handleClose() {
      console.log('关闭')
      this.$emit('handleCancel');
    }
  },

}
</script>

<style lang="scss" scoped>
@import "./assets/style";
</style>

